<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/city-picker/css/city-picker.css}" media="all">

    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="add">
    <div class="layui-form-item" >
        <label class="layui-form-label required">ID</label>
        <div class="layui-input-block">
            <input id="plantId" readonly="readonly" type="text" name="plantId" value="" class="layui-input">
            <tip>ID不可修改</tip>
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label required">作品名</label>
        <div class="layui-input-block">
            <input id="plantName" type="text" name="plantName" lay-verify="required" placeholder="植物名" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">学名</label>
        <div class="layui-input-block">
            <input id="plantSpecific" type="text" name="plantSpecific"
                   placeholder="学名" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">拉丁名</label>
        <div class="layui-input-block">
            <input id="latinName" type="text" name="latinName"
                   placeholder="拉丁名" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">植物图片</label>
        <div>
            <img  style="width: 120px;height: 100px" class="layui-form-label layui-upload-img" id="demo1">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">类群</label>
        <div class="layui-input-block">
            <select name="categoryId" lay-verify="required" lay-filter="category">
                <option value="">--------</option>
                <option th:each="item : ${clist}"
                        th:value="${item.categoryId}"
                        th:text="${item.categoryName}"
                        th:selected="${#strings.equals(item.categoryId,param.categoryId)}"
                        ></option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">科</label>
        <div class="layui-input-block">
            <select id="familySelect" name="familyId" lay-filter="family">
                <option value="">--------</option>
                <option th:each="item : ${flist}"
                        th:value="${item.familyId}"
                        th:text="${item.familyName}"
                        th:selected="${#strings.equals(item.familyId,param.familyId)
                        or #strings.equals(item.familyId,parentId)}"
                ></option>
                <!--or #strings.equals(item.familyId,${parentId})-->
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">属</label>
        <div class="layui-input-block">
            <select id="genusSelect" name="genusId" lay-filter="genus">
                <option value="">--------</option>
                <option th:each="item : ${glist}"
                        th:value="${item.familyId}"
                        th:text="${item.familyName}"
                        th:selected="${#strings.equals(item.familyId,param.familyId)}"
                ></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">植物详情</label>
        <div class="layui-input-block">
            <textarea id="plantDescription"  name="plantDescription" placeholder="描述信息"  class="layui-input">
            </textarea>
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label required">拍摄地址</label>
        <div class="layui-input-block">
            <input id="address" readonly="readonly" type="text" name="address" value="" class="layui-input">
            <tip>拍摄地址不可修改</tip>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认修改</button>
        </div>
    </div>

</div>
<script th:src="@{/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>

<script>
    layui.use(['form','upload'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        // 监听下拉框操作
        form.on('select(category)', function (data) {
            var key = data.value;
            if (key !== "") {
                $.get({
                    url: "/admin/category/show/" + key,
                    success: function (d) {
                        // console.log(d);
                        myData = d.data
                        // console.log(myData);
                        var tmp = '<option value="">--------</option>';
                        for (var i in myData) {
                            // console.log(i);
                            tmp += '<option value="' + myData[i].familyId + '">' + myData[i].familyName + '</option>';
                        }
                        $("#familySelect").html(tmp);
                        form.render();
                    }, error: function () {
                        layer.alert('请求失败');
                    }
                });
            }else {
                form.render();
            }
        });
        // 监听下拉框操作
        form.on('select(family)', function (data) {
            var key = data.value;
            if (key !== "") {
                $.get({
                    url: "/admin/category/family/show/genus/" + key,
                    success: function (d) {
                        myData = d.data
                        var tmp = '<option value="">--------</option>';
                        for (var i in myData) {
                            tmp += '<option value="' + myData[i].familyId + '">' + myData[i].familyName + '</option>';
                        }
                        $("#genusSelect").html(tmp);
                        form.render();
                    }, error: function () {
                        layer.alert('请求失败');
                    }
                });
            }else {
                form.render();
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert("点击确定修改!", {
                title: '确认提交'
            }, function () {
                // console.log(data.field)

                $.post({
                    url:"/admin/works/update",
                    data:data.field,
                    success:function (data){
                        // 关闭弹出层
                        layer.close(index);
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        // 关闭后刷新父页面
                        parent.location.reload();
                        parent.layer.close(iframeIndex);
                    }
                })

            });
            return false;
        });

    });
</script>
</body>
</html>